import "dotenv/config";
import { HumanMessage } from "@langchain/core/messages";
import { ChatOpenAI } from "@langchain/openai";
import { StringOutputParser } from "@langchain/core/output_parsers";

// import { LangchainTrace } from "langsmith";
// import { traceable } from 'langsmith/traceable'
// import { ChatOllama } from "@langchain/ollama";
// import { Ollama } from "@langchain/community/llms/ollama";
// import { ChatOllama } from "@langchain/community/chat_models/ollama";

// console.log(process.env);

const chatbot = new ChatOpenAI({
  configuration: {
    baseURL: process.env.OPENAI_API_BASE,
    apiKey: "123",
  },
});

const chatbotFallback = new ChatOpenAI({
  configuration: {
    baseURL: process.env.OPENAI_API_BASE,
    apiKey: process.env.OPENAI_API_KEY,
  },
});

const chatbotFallback1 = chatbot.withFallbacks({
  fallbacks: [chatbotFallback],
});

const outputParser = new StringOutputParser(); // 因为我们添加了 `OutputParser`，所以输出就是一个普通的文本，而不是 OpenAI 返回复杂的对象。
const simpleChain = chatbotFallback1.pipe(outputParser);

const msg = await simpleChain.invoke([new HumanMessage("你好，langchain")]);
// const [fMsg, sMsg] = await simpleChain.batch([
//   [new HumanMessage("我想学习langsmith")],
//   [new HumanMessage("你好，langchain")],
// ]);
// const stream = await simpleChain.stream([
//   new HumanMessage("我想学习langsmith"),
// ]);

// for await (const msg of stream) {
//   console.log(msg);
// }

// const streamLog = await simpleChain.streamLog([
//   new HumanMessage("我想学习langsmith"),
// ]);
// for await (const msg of streamLog) {
//   console.log(msg);
// }

// console.log(fMsg);
// console.log(sMsg);
console.log(msg);
